package com.fourforfo.fourmall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.fourforfo.fourmall.order.vo.alipay.PayVO;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016100100643028";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCR9HQGM82qLKFhZqvq498bu/ZEJN6Q2vKgKG1XxNa2sUW1Gy+Zz4oiCiydjGzBG+k2VjwbFMkPlYMd29vJdP77HszxG9ZPUxRpWaT3cQlPOVZXhGa9zHLv4Nqt2dzqXqJfXab3wy1JGoTudoV71JvRSrfq9LZ+XVpbmpQ3r4z4OWw4bY/fh8rHXNh1+n+S0SDumQXsTHdoHTAfImr+jvyv7H1ePxm1PJQLci6gTDZZvEe8csIJhE4id+3PPOL9seyaQViViIXQmYWN2SLWcA6Hs/AiCsyOPjZ4luNd93KzbPd0IP0YcGugvzqiK8i5UlWkww6TRdDacNqtGMWOsErfAgMBAAECggEAHBlM0PjeKlnnQ8IpbNIr3MIF+j6C3q5YMX829cO+dnOQcPpn4puzAzSkPDyRn2fJMR3O+oO45ZgqIvB10sYEZMIcQHrUencLIfERMlX/rdHjDnw972JR6l6GmxurTS+k4Ok9wMtBh6ZkWyhUMG/RA6z31XMBQzijsakDyeixIgRXiapU04l7sun0RoXd4WMNpcA0iwadd2GMK/6kuLiCxZlK0Y1cwxyVmaspwIuEav0b4rRiZFSe6qC+nAPSN6CUinTDp1Xfj5AagLbCdhpo6WgWnc2KZ2T5A3bSn17rS56M/IX27+D7MECzsrLmb26wbFl9P4ebohxLCpTbUAk1QQKBgQD1E2XoSqNYVoYqKci5vZG/ad4GPWxui4Irsd+2ZX/Ravs+2KVn6lEOO9CAutFgDo2yMJsSyfMI6TPM2Faztegb3hzHxlSzkAnJ9trFErq5yGD3AqjmutYfkrMhTnxDr9KIfEvnimlXyJS7HO4R3NiwUSukBJ6pKn3D3PiHsc3pPwKBgQCYdfg+lUWHwfHyZtpLRpbe/w/3uYYzTwVjEazEekZEU0lBD7DmZS2xy3oVtNbK4WD3jRhTqiapShK85gy1+imfKl9nmSOSMAL1dfnwgVJgQWyUKU0TcHG4DaQqy0FDIYOTFTvS57Dx2o/9eSLTxJIovp+Z7yfPp4Konr3wNwCWYQKBgBO/kWmrgggfSMZjAZK+DN2QyVxVXMR3p3Cf77gB68/TQBWrXYDHJ+rutPl6lF6aLZ7bPdPAxD3GlO7/y9TFZsPAHS+X8sFZbQCCTNSukq9fzzh4VWXqTlDTAG5Bpc/KfjuTUIaC58Q0HJALX7++vRNQJyUbXjGhg+rhhoFguYf/AoGAbYaqTQNdjPamgtai0dsnPZIM/ZEZOLj3DMHA1PFmk2UlxgiotAtRtV2RGhDtEZix2YVSSaB937YoGe5GiWDrD74YBcu9AyNcrMwRP1cYP7AgeZvmFCX+fBPIsTmNa9WiuUJS1OO/eS/jq1ufynR3769p5uMpPYOAWPSZTP2mfWECgYAw5o+LK67eL3DSLuYxS7ELctk2khzooexHy2g2NT8vipsE/tLx6XPdAzZtDLiWhkJRd+cDrdzotz1nkmpQuxGLelcI6wIP/E+iScORg7H3HNlg0JZkIg9ALp8eX2m+myq0BuC+JUQLQHb8Voy69EWK+i1FD1WaTHS0JRBNgB9YOg==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhJAzT6vDC663q9GRHYzUYZlkwdpIpfOrxdE0k8C/yS4b3nlztTNXkqGgGXaaXS3Me1QnUdlHn048bOJl1tzs4IHFcitQQtqLpATuOiD3p1qWos0WK2UDdI/nRiOcuu3ZK34syJNmHTYuJq4uM3fn/WrIWcw7rREmE3MrwAG6xdd6cArysdZCtAIVLyfJHhqo1rqR7W7JRMMeChAgQjqL9k1TyUsWXhYmmcXpBk0DfYImZfCA3SGwyQ5OMf6QfHJGd7XEd8014zEjdOC9NQI/zAwU6rXz6B43q0My3dbwGLtgI0cxaxblvfeEcC76s+iYd8vPMnaXGHGUlvxX6i5BQwIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="https://2347t823h4.zicp.fun/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://order.fourmall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVO vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
//        System.out.println("支付宝的响应："+result);

        return result;

    }
}
